package ui;

import security.acl.AccessController;
import security.db.DbMgr;
import core.access.Read;
import core.cwm.COI;
import core.cwm.CompanyDataSet;
import core.cwm.CompanyRecord;
import core.obj.Invoice;
import core.obj.Payment;
import core.obj.StaffingReq;
import core.obj.TimeReport;
import core.sub.Company;
import core.sub.Contractor;
import core.sub.ContractorAsEmployee;
import core.sub.Employee;

public class TestCase {
	public TestCase() {
		
	}
	
	public void createDatabase() {
		DbMgr.getInstance().initDatabase();
	}
	
	public void initialize() {
		String[] companies = {"C1","C2","C3","C4","C5","C6"};
		String[] contractors = {"Ct1","Ct2","Ct3","Ct4","Ct5","Ct6","Ct7"};
		String[] employees = {"E1","E2","E3","E4","E5","E6","E7","E8","E9"};
		String[] contractorasemployees = {"CE1","CE2"};
		String[] COIs = {"COI1","COI2","COI3"};
		
		AccessController acl = AccessController.getInstance();
		
		// Initialize companies
		Company[] company = new Company[companies.length];
		for(int i=0;i<companies.length;i++) {
			company[i] = new Company();
			company[i].setID(companies[i]);
			acl.addSubject(company[i]);
		}
		
		// Initialize COIs
		COI COI1 = new COI(COIs[0]);
		COI1.add(company[0]);
		COI1.add(company[4]);
		acl.addCOI(COI1);
		
		COI COI2 = new COI(COIs[1]);
		COI2.add(company[1]);
		COI2.add(company[2]);
		acl.addCOI(COI2);

		COI COI3 = new COI(COIs[2]);
		COI3.add(company[2]);
		COI3.add(company[3]);
		COI3.add(company[4]);
		COI3.add(company[5]);
		acl.addCOI(COI3);

		// Initialize contractors
		Contractor[] contractor = new Contractor[contractors.length];
		for(int i=0;i<contractor.length;i++) {
			contractor[i] = new Contractor();
			contractor[i].setID(contractors[i]);
			acl.addSubject(contractor[i]);
		}
		// Initialize the job history of contractor
		acl.addSubWorkForCompany(contractor[0], company[0]);
		acl.addSubWorkForCompany(contractor[0], company[1]);
		
		acl.addSubWorkForCompany(contractor[1], company[2]);
		acl.addSubWorkForCompany(contractor[1], company[3]);
		
		acl.addSubWorkForCompany(contractor[2], company[4]);
		
		acl.addSubWorkForCompany(contractor[3], company[1]);
		acl.addSubWorkForCompany(contractor[3], company[2]);
		
		acl.addSubWorkForCompany(contractor[4], company[0]);
		acl.addSubWorkForCompany(contractor[4], company[3]);
		
		acl.addSubWorkForCompany(contractor[5], company[5]);
		acl.addSubWorkForCompany(contractor[5], company[0]);
		acl.addSubWorkForCompany(contractor[5], company[2]);
		
		// Initialize employees
		Employee[] employee = new Employee[employees.length];
		for(int i=0;i<employee.length;i++) {
			employee[i] = new Employee();
			employee[i].setID(employees[i]);
			acl.addSubject(employee[i]);
		}
		
		// Initialize contractors as employees
		ContractorAsEmployee[] eascontractor = new ContractorAsEmployee[contractorasemployees.length];
		for(int i=0;i<eascontractor.length;i++) {
			eascontractor[i] = new ContractorAsEmployee();
			eascontractor[i].setID(contractorasemployees[i]);
			acl.addSubject(eascontractor[i]);
		}
		acl.addSubWorkForCompany(eascontractor[0], company[0]);
		acl.addSubWorkForCompany(eascontractor[1], company[1]);
		acl.addSubWorkForCompany(eascontractor[1], company[0]);
		
		// Initialize company data
		CompanyDataSet CD_C1 = new CompanyDataSet();
		CD_C1.addStaffReq(new StaffingReq("SR1"));
		CD_C1.addTimeReport(new TimeReport("TR1"));
		CD_C1.addInvoice(new Invoice("I1"));
		CD_C1.addPayment(new Payment("PM1"));
		CompanyRecord.getInstance().addCompanyDataSet(company[0], CD_C1);
		acl.addCompanyData(company[0], CD_C1);
		
		CompanyDataSet CD_C2 = new CompanyDataSet();
		CD_C2.addStaffReq(new StaffingReq("SR2"));
		CD_C2.addTimeReport(new TimeReport("TR2"));
		CD_C2.addInvoice(new Invoice("I2"));
		CD_C2.addPayment(new Payment("PM2"));
		CompanyRecord.getInstance().addCompanyDataSet(company[1], CD_C2);
		acl.addCompanyData(company[1], CD_C2);
		
		CompanyDataSet CD_C3 = new CompanyDataSet();
		CD_C3.addStaffReq(new StaffingReq("SR3"));
		CD_C3.addTimeReport(new TimeReport("TR3"));
		CD_C3.addInvoice(new Invoice("I3"));
		CD_C3.addPayment(new Payment("PM3"));		
		CompanyRecord.getInstance().addCompanyDataSet(company[2], CD_C3);
		acl.addCompanyData(company[2], CD_C3);
		
		CompanyDataSet CD_C4 = new CompanyDataSet();
		CD_C4.addStaffReq(new StaffingReq("SR4"));
		CD_C4.addTimeReport(new TimeReport("TR4"));
		CD_C4.addInvoice(new Invoice("I4"));
		CD_C4.addPayment(new Payment("PM4"));
		CompanyRecord.getInstance().addCompanyDataSet(company[3], CD_C4);
		acl.addCompanyData(company[3], CD_C4);
		
		CompanyDataSet CD_C5 = new CompanyDataSet();
		CD_C5.addStaffReq(new StaffingReq("SR5"));
		CD_C5.addTimeReport(new TimeReport("TR5"));
		CD_C5.addInvoice(new Invoice("I5"));
		CD_C5.addPayment(new Payment("PM5"));
		CompanyRecord.getInstance().addCompanyDataSet(company[4], CD_C5);
		acl.addCompanyData(company[4], CD_C5);
		
		CompanyDataSet CD_C6 = new CompanyDataSet();
		CD_C6.addStaffReq(new StaffingReq("SR6"));
		CD_C6.addTimeReport(new TimeReport("TR6"));
		CD_C6.addInvoice(new Invoice("I6"));
		CD_C6.addPayment(new Payment("PM6"));		
		CompanyRecord.getInstance().addCompanyDataSet(company[5], CD_C6);
		acl.addCompanyData(company[5], CD_C6);		
		
		acl.access(employee[0], CompanyRecord.getInstance().getCompanyDataSet(company[0]).getStaffReqs().get(0), Read.getRight());
		acl.access(employee[0], CompanyRecord.getInstance().getCompanyDataSet(company[1]).getStaffReqs().get(0), Read.getRight());
		
		acl.access(employee[1], CompanyRecord.getInstance().getCompanyDataSet(company[1]).getStaffReqs().get(0), Read.getRight());
		acl.access(employee[1], CompanyRecord.getInstance().getCompanyDataSet(company[5]).getStaffReqs().get(0), Read.getRight());
		
		acl.access(employee[2], CompanyRecord.getInstance().getCompanyDataSet(company[2]).getStaffReqs().get(0), Read.getRight());
		acl.access(employee[2], CompanyRecord.getInstance().getCompanyDataSet(company[0]).getStaffReqs().get(0), Read.getRight());
		
		acl.access(employee[3], CompanyRecord.getInstance().getCompanyDataSet(company[0]).getStaffReqs().get(0), Read.getRight());
		
		acl.access(eascontractor[0], CompanyRecord.getInstance().getCompanyDataSet(company[2]).getStaffReqs().get(0), Read.getRight());
		
		acl.access(eascontractor[1], CompanyRecord.getInstance().getCompanyDataSet(company[3]).getStaffReqs().get(0), Read.getRight());
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		TestCase testCase = new TestCase();
		testCase.createDatabase();
		testCase.initialize();
	}

}
